#!/bin/sh
#Barry Kauler www.puppylinux.com
#LGPL 2007 Puppy Linux www.puppylinux.com
#v3.97 handle /dev/hd also for retro kernels.
#v4.01 19may2008 BK: bugfix for 2.6.25.4, ram entries in /proc/partitions.
#100912 no longer supporting /proc/ide.
#101127 debug output.
#110125,110126 brought back /proc/ide /dev/hd* support.
#120602 kernel 3.2.18 major deviance from earlier kernels, /proc/partitions (and /sys/block) now shows /dev/sr0 when no disk.
#       new situation, getting duplicate /dev/sr0 so need 'sort -u'.
#130127 related to 120602, also screen sr0-9 out of /proc/partitions -- this may fix some problems, see main 'probepart'.
#130128 support 'udf' f.s. also see main 'probepart'.

#***this one runs in initrd, very slightly different from the main one.***

SUNITS="$1" #allowed params are '-k' or '-m'.
OUTPUT=""

##devices that have partitions... 
#([^k] is to eliminate mmcblk0 device -- allow mmcblk0p1 etc) v4.01 bugfix eliminate ram... 110126
#130127 [^kr][0-9] screens out sr0-9... (note, unfortunately will also screen out sdr0-9)
PARTITIONS="`grep -E '^ .*[^kr][0-9]$' /proc/partitions | tr -s ' ' | cut -f 4-5 -d ' ' | grep -vE ' loop| ram'`"
echo "PARTITIONS=${PARTITIONS}" > /tmp/probepart.log #101127
PARTNAMES="`echo "$PARTITIONS" | cut -f 2 -d ' '`" #120602

#101127 for comparison, log this... 110125 restore hd...
SYSBLOCKSUB="`ls -1 /sys/block/*/ | grep -E '^hd|^scd|^sd|^mmc|^sr|^nvme' | tr '\n' ' '`"
echo "SYSBLOCKSUB=${SYSBLOCKSUB}" >> /tmp/probepart.log #101127

#all disk devices...
ALLDRVS="`ls -1 /sys/block | grep -E '^scd|^sd|^mmc|^sr|^nvme'`"
[ -e /proc/ide ] && ALLDRVS="${ALLDRVS}
`ls -1 /proc/ide | grep '^hd'`" #110125 120602
echo "ALLDRVS=${ALLDRVS}" >> /tmp/probepart.log #101127 for debugging.

#all drives and partitions... 120602 sort... crap busybox sort does not support -V
ALLDEVS="`echo "${PARTNAMES}
${ALLDRVS}" | sort -u | tr '\n' ' '`"

for ONEDEV in $ALLDEVS
do
 MEDIATYPE="" #130128
 FSTYPE="unknown"
 SIZE=`echo "$PARTITIONS" | grep " ${ONEDEV}$" | cut -f 1 -d ' '`
 DEVICE="`echo "$PARTITIONS" | grep " ${ONEDEV}$" | cut -f 2 -d ' '`"
 echo "ONEDEV=${ONEDEV} SIZE=${SIZE} DEVICE=${DEVICE}" >> /tmp/probepart.log #101127 for debugging.
 if [ ! $SIZE ];then
  [ "`echo "$PARTITIONS" | grep "$ONEDEV"`" != "" ] && continue
  #must be a device without partitions...
  SIZE=0
  DEVICE="$ONEDEV"
  case $DEVICE in
   hd*) [ "`cat /proc/ide/$DEVICE/media`" = "cdrom" ] && MEDIATYPE="optical" ;; #130128
   scd*|sr*) MEDIATYPE="optical" ;; #130128 #usb,sata,scsi cd/dvd drive.
  esac
  if [ "$MEDIATYPE" != "optical" ];then #130128
   #for hd* or sd* superfloppy, determine size...
   BSIZE=`fdisk -l /dev/$DEVICE | grep "^Disk " | tr s ' ' | cut -f 7 -d ' '`
   [ $BSIZE ] && SIZE=$(($BSIZE/1024)) #KB
  fi
 fi
 FSTYPE="`blkid /dev/$DEVICE 2>/dev/null | grep -o ' TYPE=".*"' | cut -f 2 -d '"'`" #'geany 130526
 [ ! "$FSTYPE" ] && FSTYPE="unknown"
 echo "  SIZE=${SIZE} FSTYPE=${FSTYPE}" >> /tmp/probepart.log #101127 for debugging.
  
 #(using makebootfat to setup a USB-FLOPPY/-HDD/-ZIP combined bootable FAT drive).
 xFSTYPE=''
 if [ "$FSTYPE" = "unknown" ];then
  xDEVICE="`echo -n "$DEVICE" | sed 's|[0-9]*$||'`" #remove partition number.
  xFSTYPE="`fdisk -l /dev/$xDEVICE 2>/dev/null | grep "^/dev/$DEVICE " | head -n 1 | grep -o -E 'FAT12$|FAT16$|FAT32$'`"
  [ "$xFSTYPE" != "" ] && FSTYPE='vfat'
 fi
 
 [ "$FSTYPE" = "unknown" ] && FSTYPE="none"
 
 [ "$SUNITS" = "" ] && SIZE=$(($SIZE*2)) #512 byte blocks.
 [ "$SUNITS" = '-m' ] && SIZE=$(($SIZE/1024)) #MB

 echo "/dev/$DEVICE|$FSTYPE|$SIZE"
 echo "  /dev/$DEVICE|$FSTYPE|$SIZE" >> /tmp/probepart.log #101127
done

###END###
